package com.menu.Controller;

import com.menu.Pojo.SecurityUser;
import com.menu.Service.OrderService;
import com.menu.Utils.CommonUtil;
import com.menu.Utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


@Api(tags = "订单接口")
@RestController
public class OrderContoller {

    @Autowired
    private OrderService orderService;
    @Autowired
    private CommonUtil commonUtil;

    @ApiOperation("生成订单")
    @PostMapping("/Order/generateOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",value = "商品id",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "goodsNumber",value = "商品个数",dataTypeClass = Integer.class)
    })
    public ResponseResult generateOrder(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser, Integer goodsId, Integer goodsNumber,Integer addressId){
        if(goodsId == null){
            return ResponseResult.error("商品信息有误！");
        }
        if (goodsNumber == null || goodsNumber <= 0){
            return ResponseResult.error("商品数量有误！");
        }
        if (addressId == null){
            return ResponseResult.error("未选择配送地址");
        }
        return orderService.generateOrder(goodsId,goodsNumber,securityUser.getUserId(),addressId);
    }

    @ApiOperation("支付订单")
    @PostMapping("/Order/payForOrder")
    @ApiImplicitParam(name = "orderId",value = "订单Id",dataTypeClass = String.class)
    public ResponseResult payForOrder(Long orderId, @ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        if (orderId == null){
            return ResponseResult.error("参数不合法！");
        }
        return orderService.payForOrder(orderId,securityUser.getUserId());
    }

    @ApiOperation("申请退款")
    @GetMapping("/Order/applyForRefund")
    public ResponseResult applyForRefund(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,Long orderId){
        if (orderId == null){
            return ResponseResult.error("参数不合法！");
        }
        return orderService.applyForRefund(securityUser.getUserId(),orderId);
    }

    @ApiOperation("根据订单号查询")
    @GetMapping("/Admin/queryByOrder")
    public ResponseResult queryByOrder(Long orderId){
        if (orderId == null){
            return ResponseResult.error("参数有误！");
        }
        return orderService.queryByOrder(orderId,null);
    }

    @ApiOperation("根据订单号查询")
    @GetMapping("/User/queryByOrder")
    public ResponseResult UqueryByOrder(Long orderId,@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        if (orderId == null){
            return ResponseResult.error("参数有误！");
        }
        return orderService.queryByOrder(orderId,securityUser.getUserId());
    }

    @ApiOperation("管理员遍历订单")
    @GetMapping("/Admin/queryOrderByPage")
    @ApiImplicitParam(name = "state",value = "状态 0 未完成 1完成 2超时取消 3未发货 4申请退款 5已发货 6退款成功")
    public ResponseResult queryOrderByPage(Integer pageStart,Integer pageSize,Integer state){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (state != null && (state < 0 || state > 6)){
            state = null;
        }
        return orderService.queryOrderByPage(pageStart,pageSize,null,state);
    }

    @ApiOperation("用户遍历自己的遍历订单")
    @GetMapping("/User/queryOrderByPage")
    @ApiImplicitParam(name = "state",value = "null是所有订单 状态 0 未完成 1完成 2超时取消 3未发货 4申请退款 5已发货 6退款成功")
    public ResponseResult UqueryOrderByPage(Integer pageStart,Integer pageSize,Integer state,
                                            @ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (state != null && (state < 0 || state > 6)){
            state = null;
        }
        return orderService.queryOrderByPage(pageStart,pageSize,securityUser.getUserId(),state);
    }

    @ApiOperation("管理员修改订单状态")
    @PutMapping("/Admin/updateOrder")
    public ResponseResult updateOrder(Integer state,Long orderId){
        if (state == null && (state < 0 || state > 6)){
            return ResponseResult.error("参数有误！");
        }
        if (orderId == null){
            return ResponseResult.error("订单ID不合法！");
        }
        return orderService.updateOrder(state,orderId);
    }

    @ApiOperation("管理员发货")
    @PostMapping("/Admin/deliverGoods")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "订单号"),
            @ApiImplicitParam(name = "waybill",value = "运单号"),
            @ApiImplicitParam(name = "logisticsCompany",value = "物流公司")
    })
    public ResponseResult deliverGoods(Long orderId,Long waybill,
                                       String logisticsCompany){
        if (orderId == null){
            return ResponseResult.error("订单号有误！");
        }
        if (waybill == null){
            return ResponseResult.error("运单号有误！");
        }
        if (!commonUtil.verifyStirng(logisticsCompany)){
            return ResponseResult.error("快递公司有误！");
        }
        return orderService.deliverGoods(orderId,waybill,logisticsCompany);
    }

    @ApiOperation("用户签收")
    @PutMapping("/User/finishOrder")
    public ResponseResult finishOrder(Long orderId,@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        if (orderId == null) {
            return ResponseResult.error("订单号有误！");
        }
        return orderService.finishOrder(orderId,securityUser.getUserId());
    }
}
